jQuery(document).ready(function () {
    //1.初始化Table
    var toolRepairInFactoryTable = new ToolRepairInFactoryTableInit();
    var toolRepairDelegationTable= new ToolRepairDelegationTableInit();
    var toolRepairedTableInit= new ToolRepairedTableInit();
    var toolScrapTableInit= new ToolScrapTableInit();
    toolRepairInFactoryTable.Init();
    toolRepairDelegationTable.Init();
    toolRepairedTableInit.Init();
    toolScrapTableInit.Init();

    toolRepairInit.init();
    toolRepairInFactoryInit.init();
    toolRepairDelegationInit.init();
});

var toolRepairInit = function () {
    return {
        init: function () {
            SystemCommon.getSysDict("TOOL_TYPE","tool-type");
            toolRepairInit._query();
            toolRepairInit._keyQuery();
            toolRepairInit._reset();
            toolRepairInit._scrap();
            toolRepairInit._initUserSelect2();
            toolRepairInit._initDateControl();
            toolRepairInit._initToolCodeQuerySelect2();
            toolRepairInit._initEquipCodeSelect2();
        },
        _query: function () {
            $("#queryToolRepair").on("click", function () {
                $('#toolRepairInFactoryTable').bootstrapTable('refreshOptions', {
                    pageNumber: 1
                });
                $('#toolRepairDelegationTable').bootstrapTable('refreshOptions', {
                    pageNumber: 1
                });
                $('#toolRepairedTable').bootstrapTable('refreshOptions', {
                    pageNumber: 1
                });
                $('#toolScrapTable').bootstrapTable('refreshOptions', {
                    pageNumber: 1
                });
            });
        },
        _keyQuery: function () {
            $(".tool-check-search").keypress(function (e) {
                if (e.which === 13) {
                    $("#queryToolRepair").trigger("click");
                }
            });
        },
        _reset: function () {

            $(".reset").on("click", function () {
                $(".clearfix input").val("");
                $("#toolCode").val(null).trigger("change");
                $("#equipCode").val(null).trigger("change");
                $("#toolType").val("");

            });

        },
        _refreshTable:function () {
            $('#toolRepairInFactoryTable').bootstrapTable('refreshOptions', {
                pageNumber: 1
            });
            $('#toolRepairDelegationTable').bootstrapTable('refreshOptions', {
                pageNumber: 1
            });
            $('#toolRepairedTable').bootstrapTable('refreshOptions', {
                pageNumber: 1
            });
            $('#toolScrapTable').bootstrapTable('refreshOptions', {
                pageNumber: 1
            });
        },
        _initEquipCodeSelect2 : function () {
            $('#equipCode').select2({
                ajax: {
                    url: ctx + '/measureTool/measureToolInfo/getEquipmentList/get.mvc',
                    dataType: 'json',
                    data: function (params) {
                        // Query parameters will be ?search=[term]&page=[page]
                        return {
                            search: params.term,
                            page: params.page || 1,
                            size: 10
                        };
                    },
                    processResults: function (response) {
                        // Tranforms the top-level key of the response object from 'items' to 'results'
                        var test = [];
                        if (response.data) {
                            var _data = response.data;
                            for (var int = 0; int < _data.length; int++) {
                                var obj = {};
                                obj.id = _data[int].equipCode;
                                obj.text = _data[int].equipName;
                                test.push(obj);
                            }
                        }
                        return {
                            results: test
                        };
                    },
                    // Additional AJAX parameters go here; see the end of this chapter for the full code of this example
                    cache: true

                },
                placeholder: '请选择',
                language: "zh-CN",
                escapeMarkup: function (markup) {
                    return markup;
                }, // let our custom formatter work
                minimumInputLength: 0
            });
        },
        _initToolCodeQuerySelect2:function () {
            var select2 = $('#toolCode').select2({
                ajax: {
                    url: ctx +'/tool/toolInfo/listToolInfoByFilter/get.mvc',
                    dataType: 'json',
                    data: function (params) {
                        // Query parameters will be ?search=[term]&page=[page]
                        return {
                            toolCode: params.term,
                            num: params.page || 1,
                            size: 10
                        };
                    },
                    processResults: function (response,params) {
                        // Tranforms the top-level key of the response object from 'items' to 'results'
                        params.page = params.page || 1;
                        var test =[];
                        if(response.rows) {
                            var _data = response.rows;
                            for ( var int = 0; int < _data.length; int++) {
                                var obj= {};
                                obj.id=_data[int].pkId;
                                obj.text=_data[int].toolCode;
                                test.push(obj);
                            }
                        }
                        return {
                            results: test,
                            pagination :{
                                more:(params.page * 10) < response.total
                            }
                        };
                    },
                    // Additional AJAX parameters go here; see the end of this chapter for the full code of this example
                    cache: false

                },
                placeholder: '请选择',
                language: "zh-CN",
                escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
                minimumInputLength: 0
            });
            return select2;
        },
        _scrap: function () {
            $("#scrap3").on("click", function () {
                var toolCodes = $.map($('#toolRepairedTable').bootstrapTable('getSelections'), function (row) {
                    return row.toolCode;
                });
                if (toolCodes.length < 1) {
                    DialogUtil.warning("请选择需要报废的记录");
                    return false;
                }
                DialogUtil.ok_and_cancel("是否报废？", function () {
                    $.ajax({
                        url: ctx + '/tool/repair/scrap/save.mvc',
                        type: "post",
                        dataType: 'json',
                        data: {
                            toolCodes: toolCodes.toString()
                        },
                        success: function (response) {
                            console.log(response);
                            if (response.result === true) {
                                DialogUtil.warning("报废成功");
                                $('#toolRepairedTable').bootstrapTable('refresh');
                            } else {
                                DialogUtil.error(response.message);
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            DialogUtil.error(errorThrown);
                        }
                    });
                }, function () {

                });
            });
        },
        _initUserSelect2:function () {
            $('.sys-user-select2').select2({
                ajax: {
                    url: ctx +'/system/user/getSysUserList/get.mvc',
                    dataType: 'json',
                    aynsc :false,
                    data: function (params) {
                        // Query parameters will be ?search=[term]&page=[page]
                        return {
                            search: params.term,
                            num: params.page || 1,
                            size: 10
                        };
                    },
                    processResults: function (response) {
                        // Tranforms the top-level key of the response object from 'items' to 'results'
                        var test =[];
                        if(response.result === true&&response.data){
                            var _data = response.data;
                            for ( var int = 0; int < _data.length; int++) {
                                var obj={};
                                obj.id=_data[int].username;
                                obj.text=_data[int].truename;
                                test.push(obj);
                            }
                            return {
                                results: test
                            };
                        }
                        return null;
                    },
                    // Additional AJAX parameters go here; see the end of this chapter for the full code of this example
                    cache: true

                },
                placeholder: '请选择',
                language: "zh-CN",
                escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
                minimumInputLength: 0
            });
        },
        _initDateControl: function () {
            $('.form-datetime').datetimepicker({
                format: 'yyyy-mm-dd hh:ii:ss',
                language: 'zh-CN',
                autoclose: true,
                //minuteStep:1,
                pickerPosition: "bottom-left",
                //endDate : new Date(),
                forceParse: true
            }).on('hide', function (event) {
                event.preventDefault();
                event.stopPropagation();
            });
            $('.form-datetime-day').datetimepicker({
                format: 'yyyy-mm-dd',
                minView: 2,
                minuteStep:1,
                autoclose: true,
                language: 'zh-CN',
                pickerPosition: "bottom-left",
                //endDate : new Date(),
                forceParse: true
            }).on('hide', function (event) {
                event.preventDefault();
                event.stopPropagation();
            });
        }
    }
}();